using System;
using System.Collections.Generic;
using System.Text;
using FlatRedBall;
using FlatRedBall.ManagedSpriteGroups;
using FlatRedBall.Math;
using FlatRedBall.Math.Geometry;
using FlatRedBall.AI.Pathfinding;
using FlatRedBall.Graphics;
using FlatRedBall.Graphics.Animation;
using FlatRedBall.Graphics.Model;
using FlatRedBall.Graphics.Particle;

using FlatRedBall.Input;

using FlatRedBall.Instructions;
using FlatRedBall.Math.Splines;
using BitmapFont = FlatRedBall.Graphics.BitmapFont;
using Cursor = FlatRedBall.Gui.Cursor;
using GuiManager = FlatRedBall.Gui.GuiManager;
// Generated Usings
using Gnome.Screens;
using Matrix = Microsoft.Xna.Framework.Matrix;
using FlatRedBall.Broadcasting;
using Gnome.Entities;
using FlatRedBall;


#if FRB_XNA || SILVERLIGHT
using Keys = Microsoft.Xna.Framework.Input.Keys;
using Vector3 = Microsoft.Xna.Framework.Vector3;
using Texture2D = Microsoft.Xna.Framework.Graphics.Texture2D;
#endif

#if FRB_XNA
using Model = Microsoft.Xna.Framework.Graphics.Model;
#endif

namespace Gnome.Entities
{
	public partial class HUD : PositionedObject
	{
        // This is made global so that static lazy-loaded content can access it.
        public static string ContentManagerName
        {
            get;
            set;
        }

		// Generated Fields
		private static Scene Score;
		private static Scene HudFrameSpriteScene;
		public string ScoreTextDisplayText
		{
			get
			{
				return ScoreText.DisplayText;
			}
			set
			{
				ScoreText.DisplayText = value;
			}
		}
		public string BagValueDisplayText = "";
		static bool mHasRegisteredUnload = false;
		static object mLockObject = new object();

		private Text ScoreText;
		private PositionedObjectList<HudNumber> mBagValue;
		public PositionedObjectList<HudNumber> BagValue
		{
			get{ return mBagValue;}
		}
		private Scene Display;
		private Sprite mHudFrame;
		public Sprite HudFrame
		{
			get{ return mHudFrame;}
		}
		private PositionedObjectList<HudNumber> mScoreDigits;
		public PositionedObjectList<HudNumber> ScoreDigits
		{
			get{ return mScoreDigits;}
		}
		private PositionedObjectList<HudNumber> mMultiplier;
		public PositionedObjectList<HudNumber> Multiplier
		{
			get{ return mMultiplier;}
		}
		private PositionedObjectList<HudNumber> mCurrentWeight;
		public PositionedObjectList<HudNumber> CurrentWeight
		{
			get{ return mCurrentWeight;}
		}
		private PositionedObjectList<HudNumber> mMaxWeight;
		public PositionedObjectList<HudNumber> MaxWeight
		{
			get{ return mMaxWeight;}
		}
		private PositionedObjectList<HudNumber> mTimer;
		public PositionedObjectList<HudNumber> Timer
		{
			get{ return mTimer;}
		}

        Layer LayerProvidedByContainer = null;
        public HUD(string contentManagerName) :
            this(contentManagerName, true)
        {
        }


        public HUD(string contentManagerName, bool addToManagers) :
			base()
		{
			// Don't delete this:
            ContentManagerName = contentManagerName;
            Initialize(addToManagers);

		}

		protected virtual void Initialize(bool addToManagers)
		{
			// Generated Initialize
			LoadStaticContent(ContentManagerName);


			Display = Score.Clone();
			ScoreText = Display.Texts.FindByName("scoreNum");
			mBagValue = new PositionedObjectList<HudNumber>();
			mHudFrame = HudFrameSpriteScene.Sprites.FindByName("ui41").Clone();
			mScoreDigits = new PositionedObjectList<HudNumber>();
			mMultiplier = new PositionedObjectList<HudNumber>();
			mCurrentWeight = new PositionedObjectList<HudNumber>();
			mMaxWeight = new PositionedObjectList<HudNumber>();
			mTimer = new PositionedObjectList<HudNumber>();
			PostInitialize();
			if(addToManagers)
			{
				AddToManagers(null);
			}


		}

// Generated AddToManagers

        public virtual void AddToManagers(Layer layerToAddTo)
        {
			LayerProvidedByContainer = layerToAddTo;
			SpriteManager.AddPositionedObject(this);
			AddToManagersBottomUp(layerToAddTo);
			CustomInitialize();

        }

		public virtual void Activity()
		{
			// Generated Activity

			for(int i = BagValue.Count - 1; i > -1; i--)
			{
				BagValue[i].Activity();
			}
			for(int i = ScoreDigits.Count - 1; i > -1; i--)
			{
				ScoreDigits[i].Activity();
			}
			for(int i = Multiplier.Count - 1; i > -1; i--)
			{
				Multiplier[i].Activity();
			}
			for(int i = CurrentWeight.Count - 1; i > -1; i--)
			{
				CurrentWeight[i].Activity();
			}
			for(int i = MaxWeight.Count - 1; i > -1; i--)
			{
				MaxWeight[i].Activity();
			}
			for(int i = Timer.Count - 1; i > -1; i--)
			{
				Timer[i].Activity();
			}


			CustomActivity();
		}

		public virtual void Destroy()
		{
			// Generated Destroy
			SpriteManager.RemovePositionedObject(this);



			if(ScoreText != null)
			{
				TextManager.RemoveText(ScoreText);
			}
			while(BagValue.Count != 0)
			{
				BagValue[0].Destroy();
			}
			if(Display != null)
			{
				Display.RemoveFromManagers();
			}
			if(HudFrame != null)
			{
				SpriteManager.RemoveSprite(HudFrame);
			}
			while(ScoreDigits.Count != 0)
			{
				ScoreDigits[0].Destroy();
			}
			while(Multiplier.Count != 0)
			{
				Multiplier[0].Destroy();
			}
			while(CurrentWeight.Count != 0)
			{
				CurrentWeight[0].Destroy();
			}
			while(MaxWeight.Count != 0)
			{
				MaxWeight[0].Destroy();
			}
			while(Timer.Count != 0)
			{
				Timer[0].Destroy();
			}


			CustomDestroy();
		}

		// Generated Methods
		public virtual void PostInitialize()
		{
			ScoreTextDisplayText = "";
			BagValueDisplayText = "";
			X = 0f;
			Y = 0f;
		}
		public virtual void AddToManagersBottomUp(Layer layerToAddTo)
		{


            // We move this back to the origin and unrotate it so that anything attached to it can just use its absolute position
            float oldRotationX = RotationX;
            float oldRotationY = RotationY;
            float oldRotationZ = RotationZ;

            float oldX = X;
            float oldY = Y;
            float oldZ = Z;

            X = 0;
            Y = 0;
            Z = 0;
            RotationX = 0;
            RotationY = 0;
            RotationZ = 0;


			Display.AddToManagers(layerToAddTo);
			Display.AttachAllDetachedTo(this, true);
			SpriteManager.AddToLayer(mHudFrame, layerToAddTo);
			if(mHudFrame.Parent == null)
			{
				mHudFrame.AttachTo(this, true);
			}

            X = oldX;
            Y = oldY;
            Z = oldZ;
            RotationX = oldRotationX;
            RotationY = oldRotationY;
            RotationZ = oldRotationZ;
                		}
		public virtual void ConvertToManuallyUpdated()
		{
			SpriteManager.ConvertToManuallyUpdated(this);
			for(int i = 0; i < BagValue.Count; i++)
			{
				BagValue[i].ConvertToManuallyUpdated();
			}
			Display.ConvertToManuallyUpdated();
			SpriteManager.ConvertToManuallyUpdated(HudFrame);
			for(int i = 0; i < ScoreDigits.Count; i++)
			{
				ScoreDigits[i].ConvertToManuallyUpdated();
			}
			for(int i = 0; i < Multiplier.Count; i++)
			{
				Multiplier[i].ConvertToManuallyUpdated();
			}
			for(int i = 0; i < CurrentWeight.Count; i++)
			{
				CurrentWeight[i].ConvertToManuallyUpdated();
			}
			for(int i = 0; i < MaxWeight.Count; i++)
			{
				MaxWeight[i].ConvertToManuallyUpdated();
			}
			for(int i = 0; i < Timer.Count; i++)
			{
				Timer[i].ConvertToManuallyUpdated();
			}
		}
		public static void LoadStaticContent(string contentManagerName)
		{
			ContentManagerName = contentManagerName;
			bool registerUnload = false;
			if(!FlatRedBallServices.IsLoaded(@"content/entities/hud/score.scnx", ContentManagerName))
			{
				registerUnload = true;
				Score = FlatRedBallServices.Load<Scene>(@"content/entities/hud/score.scnx", ContentManagerName);
				FlatRedBallServices.AddNonDisposable(@"content/entities/hud/score.scnx", Score, ContentManagerName);
			}
			else
			{
				Score = (Scene)FlatRedBallServices.GetNonDisposable(@"content/entities/hud/score.scnx", ContentManagerName);
			}
			if(!FlatRedBallServices.IsLoaded(@"content/entities/hud/hudframespritescene.scnx", ContentManagerName))
			{
				registerUnload = true;
				HudFrameSpriteScene = FlatRedBallServices.Load<Scene>(@"content/entities/hud/hudframespritescene.scnx", ContentManagerName);
				FlatRedBallServices.AddNonDisposable(@"content/entities/hud/hudframespritescene.scnx", HudFrameSpriteScene, ContentManagerName);
			}
			else
			{
				HudFrameSpriteScene = (Scene)FlatRedBallServices.GetNonDisposable(@"content/entities/hud/hudframespritescene.scnx", ContentManagerName);
			}
			const bool throwExceptionIfAfterInitialize = false;
			if(throwExceptionIfAfterInitialize && registerUnload && ScreenManager.CurrentScreen != null && ScreenManager.CurrentScreen.ActivityCallCount > 0 && !ScreenManager.CurrentScreen.IsActivityFinished)
			{
				throw new InvalidOperationException("Content is being loaded after the current Screen is initialized.  This exception is being thrown because of a setting in Glue.");
			}
			if(registerUnload)
			{
				lock(mLockObject)
				{
					if(!mHasRegisteredUnload)
					{
						FlatRedBallServices.GetContentManagerByName(ContentManagerName).AddUnloadMethod("HUDStaticUnload", UnloadStaticContent);
						mHasRegisteredUnload = true;
					}
				}
			}
			CustomLoadStaticContent(contentManagerName);
		}
		public static void UnloadStaticContent()
		{
			mHasRegisteredUnload = false;
			if(Score != null)
			{
			}
			if(HudFrameSpriteScene != null)
			{
			}
		}

    }
	
	
	// Extra classes
	public static class HUDExtensionMethods
	{
	}
	
}
